/**
 * 
 */
package com.beforesunrise.project.server.plusservice.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.beforesunrise.project.server.plusservice.SearchPlusService;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

/**
 * @author ygkim
 *
 */
public class SearchPlusServiceImpl implements SearchPlusService {

	@Override
	public String searchActivitiesJSON(String query) {
		
		URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
		String resultString = null;
		
		try {
			
			StringBuffer urlStBuffer = new StringBuffer();
			
			urlStBuffer.append("https://www.googleapis.com/plus/v1/activities?query=");
			urlStBuffer.append(query);
			urlStBuffer.append("&key=AIzaSyA90ecKBR0fWEagks_1z5xzXegoFz_pkoI");
						
			URL url = new URL(urlStBuffer.toString());
			
			HTTPResponse response = urlFetchService.fetch(url);
			
			byte[] result = response.getContent();
			
			if (result !=null && result.length > 0) {
				resultString = new String(result);
			}
			
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultString;
	}

	@Override
	public String searchPeoplesJSON(String query) {

		URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
		String resultString = null;
		
		try {
			
			StringBuffer urlStBuffer = new StringBuffer();
			
			urlStBuffer.append("https://www.googleapis.com/plus/v1/people?query=");
			urlStBuffer.append(query);
			urlStBuffer.append("&key=AIzaSyA90ecKBR0fWEagks_1z5xzXegoFz_pkoI");
						
			URL url = new URL(urlStBuffer.toString());
			
			HTTPResponse response = urlFetchService.fetch(url);
			
			byte[] result = response.getContent();
			
			if (result !=null && result.length > 0) {
				resultString = new String(result);
			}
			
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultString;
	}

}
